﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Collections;

namespace DAO
{
    public class DataProvider
    {
        public OleDbConnection SqlConnect()
        {
            OleDbConnection sqlCn = null ;
            String Cnstr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = BookStore.accdb; Persist Security Info=False";
            try
            {
                sqlCn = new OleDbConnection(Cnstr);
                sqlCn.Open();
            }
            catch(System.Exception e)
            {
                throw (e);
            }
            
            return sqlCn;
        }
        private OleDbConnection conn;
        private OleDbDataAdapter adapt;
        private OleDbCommand cmd;
        private DataSet dtset = new DataSet();
        public static OleDbConnection conectDB()
        {
            String conectionStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=BookStore.accdb;Persist Security Info=True";
            OleDbConnection con = new OleDbConnection(conectionStr);
            con.Open();
            return con;
        }
        public DataTable ExecuteQuery(String str)
        {
            conectDB();
            DataTable table = new DataTable();
            adapt = new OleDbDataAdapter(str, conn);
            try
            {
                adapt.Fill(dtset);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            try
            {
                table = dtset.Tables[0];
                conn.Close();
                return table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public object ExCuteScalar(string str)
        {
            OleDbConnection conn = conectDB();
            object obj = null;
            cmd = new OleDbCommand(str, conn);
            obj = cmd.ExecuteScalar();
            conn.Close();
            return obj;
        }
        public static bool ExcuteNonQuery(string str)
        {
            OleDbConnection conn = conectDB();
            OleDbCommand cmd = new OleDbCommand(str, conn);
            cmd.CommandText = str;
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
            conn.Close();
            return true;
        }
        public OleDbDataReader comand(string sql)
        {
            conectDB();
            ArrayList ds = new ArrayList();
            OleDbCommand cmdnew = new OleDbCommand(sql, conn);
            OleDbDataReader dr = null;
            dr = cmdnew.ExecuteReader();
            return dr;
        }
      
    }
}
